package com.rljk.controller.backend;/**
 * Created by mac on 2018/7/11.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.service.CommentInManager;
import com.rljk.service.CaidanManager;
import com.rljk.service.HeadlinesManager;
import com.rljk.service.NewsDetailManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Author zhang
 * @Description  后台首页控制层
 * @Date 2018/7/23 14:51
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/backendhome")
@Slf4j
public class HomeController extends BaseController {

    @Resource(name = "headlinesService")
    private HeadlinesManager headlinesService;

    @Resource(name = "newsdetailService")
    private NewsDetailManager newsdetailService;

    @Resource(name = "commentInService")
    private CommentInManager commentInService;

    @Resource(name = "caidanService")
    private CaidanManager caidanService;

    /**
     * @Author zhang
     * @Description  后台首页列表（全部）
     * @Date 2018/7/12 15:05
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/listAll")
    @ResponseBody
    public Object homeListAll(){
        log.info("后台首页列表（全部）");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("HOMELISTALL", pd.getString("FKEY"))){	//检验请求key值是否合法
                //if(AppUtil.checkParam("taskListAll", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    List<PageData> listAll = headlinesService.getHomeInfo(pd);
                Iterator<PageData> it = listAll.iterator();
                while (it.hasNext()){
                    PageData next = it.next();
                    String newsdetailId = (String)next.get("NEWSDETAIL_ID");
                    PageData commentCount = commentInService.getCommentCount(newsdetailId);
                    next.put("COMMENT_NUM",commentCount.get("COMMENT_NUM"));
                    System.out.println(next.get("CREATE_TIME"));
                }
                return Result.success(listAll);
//                }else {
//                    return Result.error(CodeMsg.PARAM_LACK);
//                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  后台查看单个首页详细
     * @Date 2018/7/26 11:09
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/homeinfo")
    @ResponseBody
    public Object homeInfo(){
        log.info("后台查看单个首页详细");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("HOMEINFO", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("homeInfo", pd)){	//检查参数
                log.info("请求参数："+pd.toString());
                    PageData homeInfo = newsdetailService.getNewsDetailById(pd);

                    return Result.success(homeInfo);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * 后台获取菜单信息
     * @return
     */
    @RequestMapping(value="/backcaidan")
    @ResponseBody
    public Object backCaiDan(){
        log.info("后台获取菜单信息");
        PageData pd = new PageData();
        pd = this.getPageData();
        try{
            if(Tools.checkKey("BACKCAIDAN", pd.getString("FKEY"))){	//检验请求key值是否合法
                if(AppUtil.checkParam("backCaiDan", pd)){	//检查参数
                    log.info("请求参数："+pd.toString());
                    List<PageData> list = caidanService.listAll(pd);
                    if(Tools.isEmptyList(list)){
                        return Result.error(CodeMsg.EMPTY);
                    }
                    return Result.success(list);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            }else{
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        }catch (Exception e){
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

}
